<script setup lang="ts">
import type { ConsultType } from '@/enums'
import { ref } from 'vue'
import ConsultItem from './ConsultItem.vue'
import type { ConsultOrderItem, ConsultOrderListParams } from '@/types/consult'
import { reqGetConsultOrderList } from '@/services/consult'
const props = defineProps<{
  type: ConsultType
}>()

// 页面渲染的数据
const list = ref<ConsultOrderItem[]>([])
// 加载状态
const loading = ref(false)
// 没有更多了，需要用到的状态
const finished = ref(false)
// 发请求需要传入的参数
const params = ref<ConsultOrderListParams>({
  current: 1,
  pageSize: 10,
  type: props.type
})

// 滚动条与底部距离小于 offset 时触发
const onLoad = async () => {
  const res = await reqGetConsultOrderList(params.value)
  // 拿到数据需要解构push到list中去
  console.log(res)
  list.value.push(...res.data.rows)
  // 关闭loading
  loading.value = false
  // 如果页面的数据条数，大于后端的数据的最大条数时，把finshed改为true
  // pageTotal  一共多少页
  // total      总共多少条
  // current    表示第几页
  if (params.value.current >= res.data.pageTotal) {
    finished.value = true
  } else {
    params.value.current++
  }
}

// 子传父 父组件操作数据
const handleDelete = (id: string) => {
  list.value = list.value.filter((item) => item.id !== id)
}
</script>

<template>
  <!-- @load 滚动条与底部距离小于 offset 时触发 -->
  <div class="consult-list">
    <van-list
      v-model:loading="loading"
      :finished="finished"
      finished-text="没有更多了"
      @load="onLoad"
    >
      <consult-item v-for="item in list" :key="item.id" :item="item" @on-delete="handleDelete" />
    </van-list>
  </div>
</template>

<style lang="scss" scoped>
.consult-list {
  padding: 10px 15px;
}
</style>
